﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.mDashboard.BLL;
using Lamex.mDashboard.Model;

namespace Lamex.mDashboard.UI
{
    public partial class frmIE_Seat_ProcessSetting : DevExpress.XtraEditors.XtraForm
    {
        public frmIE_Seat_ProcessSetting()
        {
            InitializeComponent();
        }
        string TargetIDNO = "";
        string ProcessIDNO = "";
        string isProcessFlag = "";
        string isTargetFlag = "";
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(txtQueryType.Text))
            {
                strWhere += " and ProcessType='" + txtQueryType.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtQueryProcessCode.Text))
            {
                strWhere += " and ProcessCode='" + txtQueryProcessCode.Text + "'";
            }
            gviewProduct.DataSource = BaseFactory<B_ST_ProductDefinie>.Instance.GetEntriyList("1=1  "+strWhere);
        }

        private void btnQueryTarget_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(txtQueryHours.Text))
            {
                strWhere += " and FormalTime='" + txtQueryHours.Text + "'";
            }
            gviewTarget.DataSource = BaseFactory<B_ST_TargetValue>.Instance.GetEntriyList("1=1  " + strWhere);
        }

        private void gviewProduct_Click(object sender, EventArgs e)
        {
            int iRow = gridView1.FocusedRowHandle;
            ProcessIDNO = gridView1.GetRowCellDisplayText(iRow, "IDNO").ToString();
            txtType.Text = gridView1.GetRowCellDisplayText(iRow, "ProcessType").ToString();
            txtProcessCode.Text = gridView1.GetRowCellDisplayText(iRow, "ProcessCode").ToString();
            txtProcessName.Text = gridView1.GetRowCellDisplayText(iRow, "ProcessName").ToString();
            txtDescription.Text = gridView1.GetRowCellDisplayText(iRow, "Description").ToString();
            txtUptime.Text = gridView1.GetRowCellDisplayText(iRow, "UpTime").ToString();
            txtFlag.Text = gridView1.GetRowCellDisplayText(iRow, "Flag").ToString();
            
        }

        private void gviewTarget_Click(object sender, EventArgs e)
        {
            int iRow = gridView2.FocusedRowHandle;
           TargetIDNO = gridView2.GetRowCellDisplayText(iRow, "IDNO").ToString();
            txtHours.Text= gridView2.GetRowCellDisplayText(iRow, "FormalTime").ToString();
            txtTarget.Text = gridView2.GetRowCellDisplayText(iRow, "TargetValue").ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string isProcessFlag = "New";
            txtType.Text = "";
            txtProcessCode.Text = "";
            txtProcessName.Text = "";
            txtUptime.Text = "0";
            txtDescription.Text = "";
            txtFlag.Text = "";
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string isProcessFlag = "Edit";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(isProcessFlag))
            {
                XtraMessageBox.Show("没有要新增或编辑的的数据！");
                return;
            }
            txtType.Alter();
            txtProcessCode.Alter();
            txtProcessCode.Alter();
            txtProcessName.Alter();
            txtUptime.Alter();
            B_ST_ProductDefinieInfo model = new B_ST_ProductDefinieInfo();
            if (isProcessFlag == "New")
            {
               
                model.ProcessType = txtType.Text;
                model.ProcessCode = txtProcessCode.Text;
                model.ProcessName = txtProcessName.Text;
                model.UpTime = int.Parse(txtUptime.Text);
                model.Flag = txtFlag.Text;
                model.Description = txtDescription.Text;
                if (BaseFactory<B_ST_ProductDefinie>.Instance.Insert(model))
                {
                    XtraMessageBox.Show("保存成功");
                }
                else
                {
                    XtraMessageBox.Show("保存失败");
                }
            }
            else
            {
                
                model.IDNO = int.Parse(ProcessIDNO);
                model.ProcessType = txtType.Text;
                model.ProcessCode = txtProcessCode.Text;
                model.ProcessName = txtProcessName.Text;
                model.UpTime = int.Parse(txtUptime.Text);
                model.Flag = txtFlag.Text;
                model.Description = txtDescription.Text;
                if (BaseFactory<B_ST_ProductDefinie>.Instance.Update(model))
                {
                    XtraMessageBox.Show("保存成功");
                }
                else
                {
                    XtraMessageBox.Show("保存失败");
                }
            }
        }

        private void btnTargetSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(isTargetFlag))
            {
                XtraMessageBox.Show("没有要新增或编辑的的数据！");
                return;
            }
            txtHours.Alter();
            txtTarget.Alter();
            B_ST_TargetValueInfo model = new B_ST_TargetValueInfo();
            if (isTargetFlag == "New")
            {
               
                model.FormalTime = int.Parse(txtHours.Text);
                model.TargetValue = int.Parse(txtTarget.Text);
               
                if (BaseFactory<B_ST_TargetValue>.Instance.Insert(model))
                {
                    XtraMessageBox.Show("保存成功");
                }
                else
                {
                    XtraMessageBox.Show("保存失败");
                }
            }
            else
            {
                
                model.IDNO = int.Parse(TargetIDNO);
                model.FormalTime = int.Parse(txtHours.Text);
                model.TargetValue = int.Parse(txtTarget.Text);

                if (BaseFactory<B_ST_TargetValue>.Instance.Update(model))
                {
                    XtraMessageBox.Show("保存成功");
                }
                else
                {
                    XtraMessageBox.Show("保存失败");
                }
            }
        }

        private void btnTargetAdd_Click(object sender, EventArgs e)
        {
            isTargetFlag = "New";
            txtHours.Text = "0";
            txtTarget.Text = "0";
        }

        private void btnTargetEdit_Click(object sender, EventArgs e)
        {
            isTargetFlag = "Edit";
        }
    }
}